<?php
namespace Home\Controller;
use Think\Controller;

/**
 * CommonController
 * 通用控制器
 */
class CommonController extends Controller {
    /**
    * 全局初始化
    * @return
    */
    public function _initialize() {
        // utf-8编码
        header('Content-Type: text/html; charset=utf-8');
    }

    /**
     * httpGet
     * @param  string  $url url
     * @return json
     */
    public function httpGet($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url);
        $json =  curl_exec($ch);
        curl_close($ch);
        return $json;
    }

    /**
     * 是否存在帐号
     * @param  string  $name name
     * @return boolean
     */
    public function existAccount($name) {
        if (M('Admin')->where("name='{$name}'")->count() > 0) {
            return true;
        }

        return false;
    }

    /**
     * 加密数据
     * @param  string $data 需要加密的数据
     * @return string
     */
    public function encrypt($data) {
        return md5(C('AUTH_MASK') . md5($data));
    }

    /**
     * 账户是否启用
     * @param  string  $name name
     * @return boolean
     */
    public function isActive($name) {
        $where = array(
            'name' => $name,
            'role_id' => 3,
            'is_active' => 1
        );

        if (M('Admin')->where($where)->count() > 0) {
            return true;
        }

        return false;
    }

    /**
     * { status : true, info: $info}
     * @param  string $info
     * @param  string $url
     * @return
     */
    protected function successReturn($info, $url) {
        $this->resultReturn(true, $info, $url);
    }

    /**
     * { status : false, info: $info}
     * @param  string $info
     * @param  string $url
     * @return
     */
    protected function errorReturn($info, $url) {
        $this->resultReturn(false, $info, $url);
    }

    /**
     * 返回带有status、info键值的json数据
     * @param  boolean $status
     * @param  string $info
     * @param  string $url
     * @return
     */
    protected function resultReturn($status, $info, $url) {
        $json['status'] = $status;
        $json['info'] = $info;
        $json['url'] = isset($url) ? $url : '';

        return $this->ajaxReturn($json);
    }

    /**
	 *生成随机数
	 *@return
	 */
    protected function getRandChar($length) {
        $str = null;
        $strPol = "0123456789";
        $max = strlen($strPol)-1;

        for($i=0;$i<$length;$i++){
         $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }
        return $str;
   }

   /**
     * 得到数据分页
     * @param  string $modelName 模型名称
     * @param  array  $where     分页条件
     * @return array
     */
    protected function getPagination($modelName, $where, $fields, $order) {
        $service = D($modelName, 'Service');
        // 总数据行数
        $totalRows = $service->getCount($where);
        // 实例化分页
        $page = new \Org\Util\Page($totalRows, C('PAGE_LIST_ROWS'));
        $result['show'] = $page->show();
        // 得到分页数据
        $data = $service->getPagination($where,
                                        $fields,
                                        $order,
                                        $page->firstRow,
                                        $page->listRows);
        $result['data'] = $data;
        $result['total_rows'] = $totalRows;
        return $result;
    }

    /**
     * 调用分页
     */
    protected function getPageHtml($amount,$currentPage,$pageSize) {
		$html = '';
		$pageCount = 0;
		$listPageSize = 5;
		
		if ($amount % $pageSize) {
			$pageCount = (int)($amount / $pageSize) + 1;
		} else {
			$pageCount = $amount / $pageSize;
		}
		if ($currentPage>$pageCount) $currentPage = $pageCount;
		$startPage = $currentPage - floor($listPageSize/2);
		if ($startPage < 1) $startPage = 1;

		if ($currentPage == 1) {	//首页
			$html .= '<a class="disabled" href="javascript:void(0);">首页</a>';
			$html .= '<a class="disabled" href="javascript:void(0);">上一页</a>';
		} else {
			$html .= '<a href="javascript:void(0);" onclick="goToPage(1);">首页</a>';
			$html .= '<a href="javascript:void(0);" onclick="goToPage('.($currentPage-1).');">上一页</a>';
		}

		for($page=$startPage; $page<$startPage+$listPageSize; $page++) {
			if($page>$pageCount) break;
			if ($page == $currentPage) {
				$html .= '<span class="current">'.$page.'</span>';
			} else {
				$html .= '<a href="javascript:void(0);" onclick="goToPage('.$page.')">'.$page.'</a>';
			}
		}

		if ($currentPage == $pageCount) {	//末页
			$html .= '<a class="disabled" href="javascript:void(0);">下一页</a>';
			$html .= '<a class="disabled" href="javascript:void(0);">末页</a>';
		} else {
			$html .= '<a href="javascript:void(0);" onclick="goToPage('.($currentPage+1).');">下一页</a>';
			$html .= '<a href="javascript:void(0);" onclick="goToPage('.$pageCount.');">末页</a>';
		}

		return $html;
    }
    
    /**
     * 调用分页2
     */
    protected function getPageHtml2($amount,$currentPage,$pageSize) {
		$html = '';
		$pageCount = 0;
		$listPageSize = 5;
		
		if ($amount % $pageSize) {
			$pageCount = (int)($amount / $pageSize) + 1;
		} else {
			$pageCount = $amount / $pageSize;
		}
		if ($currentPage>$pageCount) $currentPage = $pageCount;
		$startPage = $currentPage - floor($listPageSize/2);
		if ($startPage < 1) $startPage = 1;

		if ($currentPage == 1) {	//首页
			$html .= '<a class="disabled" href="javascript:void(0);">首页</a>';
			$html .= '<a class="disabled" href="javascript:void(0);">上一页</a>';
		} else {
			$html .= '<a href="javascript:void(0);" onclick="goToPage2(1);">首页</a>';
			$html .= '<a href="javascript:void(0);" onclick="goToPage2('.($currentPage-1).');">上一页</a>';
		}

		for($page=$startPage; $page<$startPage+$listPageSize; $page++) {
			if($page>$pageCount) break;
			if ($page == $currentPage) {
				$html .= '<span class="current">'.$page.'</span>';
			} else {
				$html .= '<a href="javascript:void(0);" onclick="goToPage2('.$page.')">'.$page.'</a>';
			}
		}

		if ($currentPage == $pageCount) {	//末页
			$html .= '<a class="disabled" href="javascript:void(0);">下一页</a>';
			$html .= '<a class="disabled" href="javascript:void(0);">末页</a>';
		} else {
			$html .= '<a href="javascript:void(0);" onclick="goToPage2('.($currentPage+1).');">下一页</a>';
			$html .= '<a href="javascript:void(0);" onclick="goToPage2('.$pageCount.');">末页</a>';
		}

		return $html;
	}
}
